/**
 * 流口
 */
import { Op } from 'sequelize'

class AdminFlowPersonController extends BasicController {

    /**
     * 流口列表
     * @return {[type]} [description]
     */
    async actionIndex() {
        let { page, search, start_age, end_age, start_time, end_time, card_province, card_city } = $_POST;
        let number = page && page.number !== undefined ? page.number : 1;
        let size = page && page.size !== undefined ? page.size : 20;
        let where = {};
        if (search) {
            where = {
                [Op.or]: [{
                        real_name: {
                            [Op.like]: '%' + search + '%'
                        }
                    },
                    {
                        id_card: {
                            [Op.like]: '%' + search + '%'
                        }
                    },
                    {
                        mobile: {
                            [Op.like]: '%' + search + '%'
                        }
                    }
                ]
            }
        }
        if (start_age) {
            where = {
                [Op.and]: [
                    where,
                    {
                        age: {
                            [Op.gte]: start_age
                        }
                    }
                ]
            }
        }
        if (end_age) {
            where = {
                [Op.and]: [
                    where,
                    {
                        age: {
                            [Op.lte]: end_age
                        }
                    }
                ]
            }
        }
        if (start_time) {
            where = {
                [Op.and]: [
                    where,
                    {
                        created_time: {
                            [Op.gte]: start_time
                        }
                    }
                ]
            }
        }
        if (end_time) {
            where = {
                [Op.and]: [
                    where,
                    {
                        created_time: {
                            [Op.lte]: end_time
                        }
                    }
                ]
            }
        }
        if (card_province) {
            where = {
                [Op.and]: [
                    where,
                    {
                        card_province
                    }
                ]
            }
        }
        if (card_city) {
            where = {
                [Op.and]: [
                    where,
                    {
                        card_city
                    }
                ]
            }
        }

        return await this.modules('FlowPerson').where(where).page(number, size).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 流口详情
     * @return {[type]} [description]
     */
    async actionView() {
        let { id } = $_POST;
        let data = await this.modules('FlowPerson').where({ id }).first();
        if (data) {
            return data;
        } else {
            throw new Error('数据不存在');
        }
    }

    /**
     * 新增流口
     * @return {[type]} [description]
     */
    async actionCreate() {
        let { id_card } = $_POST;
        let check = await this.modules('FlowPerson').where({ id_card }).first();
        if (check) {
            throw new Error('用户已存在');
        }
        let data = await this.modules('FlowPerson').insert($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }

    /**
     * 编辑流口
     * @return {[type]} [description]
     */
    async actionUpdate() {
        let { id, id_card } = $_POST;
        let check = await this.modules('FlowPerson').where({ id_card }).first();
        if (check && check.id != id) {
            throw new Error('用户已存在');
        }
        let data = await this.modules('FlowPerson').where({ id }).update($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }

    /**
     * 删除流口
     * @return {[type]} [description]
     */
    async actionDelete() {
        let { id } = $_POST;
        let res = await this.modules('FlowPerson').where({ id }).delete();
        if (res) {
            return true;
        } else {
            throw new Error('删除失败');
        }
    }

}

export default AdminFlowPersonController;